package com.woniu.venue.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniu.exception.YuJiaException;
import com.woniu.exception.coachEnter.CoachEnterException;
import com.woniu.exception.coachEnter.CoachEnterExceptionCode;
import com.woniu.model.Result;
import com.woniu.util.StateUtil;
import com.woniu.venue.client.AccountClient;
import com.woniu.venue.client.model.CoachResumeDto;
import com.woniu.venue.dto.coachEnter.CoachEnterDto;
import com.woniu.venue.model.CoachEnter;
import com.woniu.venue.dao.CoachEnterDao;
import com.woniu.venue.param.coachEnter.CreatCoachEnterParam;
import com.woniu.venue.param.coachEnter.LoadCoachEnterForCoachParam;
import com.woniu.venue.param.coachEnter.LoadCoachEnterParam;
import com.woniu.venue.param.coachEnter.RefuseCoachEnterParam;
import com.woniu.venue.service.CoachEnterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年07月12日
 */
@Service
public class CoachEnterServiceImpl extends ServiceImpl<CoachEnterDao, CoachEnter> implements CoachEnterService {
    @Autowired
    private CoachEnterDao coachEnterDao;
    @Autowired
    private AccountClient accountClient;

    @Override
    @Transactional
    public CoachEnterDto creat(CreatCoachEnterParam param) {
        QueryWrapper<CoachEnter> qw = new QueryWrapper<CoachEnter>().eq("coach_id", param.getCoachId()).eq("venue_id",param.getVenueId()).eq("enter_state", StateUtil.COACH_ENTER_STATE_NORMAL);
        CoachEnter coachEnter = coachEnterDao.selectOne(qw);
        if (ObjectUtil.isNotEmpty(coachEnter))
            throw new CoachEnterException(CoachEnterExceptionCode.COACH_ENTER_HAD_EXIST);
        CoachEnter coachEnter1 = BeanUtil.toBean(param, CoachEnter.class);
        Result<CoachResumeDto> coachResumeDtoResult = accountClient.findCoachResumeById(param.getCoachId());
        if (coachResumeDtoResult.getCode() != 200) {
            throw new CoachEnterException(coachResumeDtoResult.getCode(), coachResumeDtoResult.getMsg());
        }
        CoachResumeDto data = coachResumeDtoResult.getData();
        coachEnter1.setCoachEnterDesc(JSONUtil.toJsonStr(data));
        coachEnter1.setCreateTime(LocalDateTime.now().toString());
        coachEnterDao.insert(coachEnter1);
        CoachEnterDto coachEnterDto = BeanUtil.toBean(coachEnter1, CoachEnterDto.class);
        coachEnterDto.setCoachEnterDetail(JSONUtil.toBean(coachEnter1.getCoachEnterDesc(),CoachResumeDto.class));
        return coachEnterDto;
    }

    @Override
    @GlobalTransactional(rollbackFor = Throwable.class)
    public void agree(Integer id) {
        CoachEnter coachEnter = coachEnterDao.selectById(id);
        if (ObjectUtil.isEmpty(coachEnter)) {
            throw new CoachEnterException(CoachEnterExceptionCode.COACH_ENTER_NOT_EXIST);
        }
        if (coachEnter.getEnterState() != StateUtil.COACH_ENTER_STATE_NORMAL) {
            throw new CoachEnterException(CoachEnterExceptionCode.COACH_ENTER_STATE_ERROR);
        }
        coachEnter.setEnterState(StateUtil.COACH_ENTER_STATE_AGREE);
        coachEnter.setDealTime(LocalDateTime.now().toString());
        coachEnterDao.updateById(coachEnter);
        Result result = accountClient.changeCoachState(coachEnter.getCoachId());
        if (result.getCode() != 200) {
            throw new YuJiaException(result.getCode(), result.getMsg());
        }
    }

    @Override
    public void refuse(RefuseCoachEnterParam param) {
        CoachEnter coachEnter = coachEnterDao.selectById(param.getId());
        if (ObjectUtil.isEmpty(coachEnter)) {
            throw new CoachEnterException(CoachEnterExceptionCode.COACH_ENTER_NOT_EXIST);
        }
        if (coachEnter.getEnterState() != StateUtil.COACH_ENTER_STATE_NORMAL) {
            throw new CoachEnterException(CoachEnterExceptionCode.COACH_ENTER_STATE_ERROR);
        }
        coachEnter.setDealTime(LocalDateTime.now().toString());
        coachEnter.setEnterState(StateUtil.COACH_ENTER_STATE_REFUSE);
        coachEnter.setRejectionReason(param.getReason());
        coachEnterDao.updateById(coachEnter);
    }

    @Override
    public void cancel(Integer id) {
        CoachEnter coachEnter = coachEnterDao.selectById(id);
        if (ObjectUtil.isEmpty(coachEnter)) {
            throw new CoachEnterException(CoachEnterExceptionCode.COACH_ENTER_NOT_EXIST);
        }
        if (coachEnter.getEnterState() != StateUtil.COACH_ENTER_STATE_NORMAL) {
            throw new CoachEnterException(CoachEnterExceptionCode.COACH_ENTER_STATE_ERROR);
        }
        coachEnter.setDealTime(LocalDateTime.now().toString());
        coachEnter.setEnterState(StateUtil.COACH_ENTER_STATE_CANCEL);
        coachEnterDao.updateById(coachEnter);
    }

    @Override
    public PageInfo<CoachEnterDto> load(LoadCoachEnterParam param) {
        PageHelper.startPage(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 6 : param.getPageSize());
        ArrayList<CoachEnterDto> coachEnterDtos = coachEnterDao.load(param);
        PageInfo<CoachEnterDto> coachEnterDtoPageInfo = new PageInfo<>(coachEnterDtos);
        return coachEnterDtoPageInfo;
    }

    @Override
    public PageInfo<CoachEnterDto> findCoachEnterByCoachId(LoadCoachEnterForCoachParam param) {
        PageHelper.startPage(param.getPageNum() == null ? 1 : param.getPageNum(), param.getPageSize() == null ? 6 : param.getPageSize());
        ArrayList<CoachEnterDto> coachEnterDtos = coachEnterDao.findCoachEnterByCoachId(param);
        PageInfo<CoachEnterDto> coachEnterDtoPageInfo = new PageInfo<>(coachEnterDtos);
        return coachEnterDtoPageInfo;
    }
}
